// pages/goods_classify2/goods_classify2.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classifyIndex:0,
        classify:[],
        goodsIndex: 0,
        mengShow: false,
        tanShow: false,
        count: 1,//初始选择商品的数量
        standardIndex: -1,//初始选择规格的下标
        product:[],
        product2: null,
        childId:"",
    },
    //选择分类
    chooseClassifyIndex:function(e){
        var index=e.currentTarget.id;
        var that = this;
        var classify=that.data.classify;
        var id = classify[index].id;
        var childId = classify[index].childId;
        that.setData({
            classifyIndex:index,
            childId: childId,
        })     
        //console.log(id)
        
        //获取商品列表
        that.goodsList();
    },
    // 选择规格
    chooseStandardIndex: function (e) {
        var index = e.currentTarget.id;
        this.setData({
            standardIndex: index,
        })
    },
    getCount: function (e) {
        this.setData({
            count: e.detail.value,
        })
    },
    del: function () {
        var that = this;
        var count = that.data.count;
        count--;
        if (count < 1) {
            count = 1;
            wx.showToast({
                title: '至少选择一个商品',
                icon: 'none',
            })
        }
        that.setData({
            count: count,
        })
    },
    add: function (e) {
        var that = this;
        var count = that.data.count;
        var buynum = e.currentTarget.dataset.buynum;
        if (count >= buynum) {
            count = buynum;
        } else {
            count++;
        }
        that.setData({
            count: count,
        })
    },
    buyShow: function (e) {
        var index = e.currentTarget.id;
        var that = this;
        var product = that.data.product;
        var product2 = product[index];
        that.setData({
            product2: product2,
        })
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 500,
        })
        setTimeout(function () {
            that.setData({
                goodsIndex: index,
                mengShow: true,
                tanShow: true,
                isScroll: false,
            })
        }, 500)
    },
    closeShow: function () {
        this.close();
    },
    close: function () {
        var that = this;
        that.setData({
            mengShow: false,
            tanShow: false,
            count: 1,
            standardIndex: -1,
            isScroll: true,
        })
    },
    close2: function () {
        this.setData({
            searchShow: false,
        })
    },
    addCart: function () {
        var that = this;
        var openid = wx.getStorageSync('openid');
        var product = that.data.product;
        var goodsIndex = that.data.goodsIndex;
        var standardIndex = that.data.standardIndex;
        var arr = [];
        var obj = {};
        obj.goods_id = product[that.data.goodsIndex].id;
        obj.standard_id = '0';
        if (product[that.data.goodsIndex].standard.length > 0) {
            if (that.data.standardIndex == -1) {
                wx.showToast({
                    title: '您还没有选择规格',
                    icon: 'none',
                })
            } else {
                obj.standard_id = product[goodsIndex].standard[standardIndex].id;
                obj.goods_count = that.data.count;
                arr.push(obj);
                obj = null;
                wx.request({
                    url: Host + 'Default/addcart',
                    data: { openid, item: JSON.stringify(arr) },
                    method: 'post',
                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                    success: function (res) {
                        if (res.data.code == 200) {
                            that.close();
                            wx.showToast({
                                title: '加入购物车成功',
                                icon: 'success',
                            })
                        } else {
                            wx.showToast({
                                title: res.data.errmsg,
                                icon: 'none',
                            })
                        }
                    },
                })
            }
        } else {
            obj.goods_count = that.data.count;
            arr.push(obj);
            obj = null;
            //console.log(arr)
            wx.request({
                url: Host + 'Default/addcart',
                data: { openid, item: JSON.stringify(arr) },
                method: 'post',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: function (res) {
                    if (res.data.code == 200) {
                        that.close();
                        wx.showToast({
                            title: '加入购物车成功',
                            icon: 'success',
                        })
                    } else {
                        wx.showToast({
                            title: res.data.errmsg,
                            icon: 'none',
                        })
                    }
                },
            })
        }

    },
    onceBuy: function () {
        var that = this;
        var product = that.data.product;
        var goodsIndex = that.data.goodsIndex;
        var standardIndex = that.data.standardIndex;
        var arr = [];
        var obj = {};
        obj.goods_id = product[that.data.goodsIndex].id;
        obj.standard_id = '0';
        if (product[goodsIndex].standard.length > 0) {
            if (standardIndex == -1) {
                wx.showToast({
                    title: '您还没有选择规格',
                    icon: 'none',
                })
            } else {
                obj.standard_id = product[goodsIndex].standard[standardIndex].id;
                obj.goods_count = that.data.count;
                arr.push(obj);
                obj = null;
                //console.log(arr)
                wx.setStorageSync('order', arr)
                that.close();
                wx.navigateTo({
                    url: '/pages/order/order',
                })
            }
        } else {
            obj.goods_count = that.data.count;
            arr.push(obj);
            obj = null;
            //console.log(arr)
            wx.setStorageSync('order', arr)
            that.close();
            wx.navigateTo({
                url: '/pages/order/order',
            })
        }
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        //获取分类
        wx.request({
          url: Host + 'Default/category',
          data: {},
          success: function (res) {
            that.setData({
              classify: res.data.list,
            });
            var childId=0;
            var classify = that.data.classify;
            var classifyIndex = that.data.classifyIndex;
            classify.forEach(item2 => {
              if (item2.isChoosed) {
                childId = item2.childId
              }
            })
            that.setData({
              childId: childId,
            });
            //获取商品列表
            that.goodsList();
          }
        })
    },
  //获取商品列表
  goodsList: function () {
    var that = this;
    var openid = wx.getStorageSync('openid');
    var childId = that.data.childId;
    that.setData({
      successShow: false,
    })
    wx.request({
      url: Host + 'Default/goodsList',
      data: { openid, childId},
      success: function (res) {
        that.setData({
          successShow: true,
        })
        that.setData({
          product: res.data.list,
        });
      },
    })
  },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})